package sunger.net.org.dropmenu.tab.utils;

import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;

import sunger.net.org.dropmenu.tab.widget.MsgComponent;

/**
 * 未读消息提示View,显示小红点或者带有数字的红点:
 * 数字一位,圆
 * 数字两位,圆角矩形,圆角是高度的一半
 * 数字超过两位,显示99+
 */
public class UnreadMsgUtils {
    private UnreadMsgUtils() {
    }

    /**
     * 显示
     *
     * @param msgComponent 子控件
     * @param num 值
     */
    public static void show(MsgComponent msgComponent, int num) {
        if (msgComponent == null) {
            return;
        }
        DependentLayout.LayoutConfig lp = (DependentLayout.LayoutConfig) msgComponent.getLayoutConfig();
        float density = msgComponent.getContext().getResourceManager().getDeviceCapability().screenDensity;
        msgComponent.setVisibility(Component.VISIBLE);
        if (num <= 0) {
            msgComponent.setStrokeWidth(0);
            msgComponent.setText("");

            lp.width = (int) (5 * density);
            lp.height = (int) (5 * density);
            msgComponent.setLayoutConfig(lp);
        } else {
            lp.height = (int) (18 * density);
            if (num < 10) {
                lp.width = (int) (18 * density);
                msgComponent.setText(num + "");
            } else if (num < 100) {
                lp.width = DependentLayout.LayoutConfig.MATCH_CONTENT;
                msgComponent.setPadding((int) (6 * density), 0, (int) (6 * density), 0);
                msgComponent.setText(num + "");
            } else {
                lp.width = DependentLayout.LayoutConfig.MATCH_CONTENT;
                msgComponent.setPadding((int) (6 * density), 0, (int) (6 * density), 0);
                msgComponent.setText("99+");
            }
            msgComponent.setLayoutConfig(lp);
        }
    }

    /**
     * 设置尺寸
     *
     * @param rtv 控件
     * @param size size
     */
    public static void setSize(MsgComponent rtv, int size) {
        if (rtv == null) {
            return;
        }
        DependentLayout.LayoutConfig lp = (DependentLayout.LayoutConfig) rtv.getLayoutConfig();
        lp.width = size;
        lp.height = size;
        rtv.setLayoutConfig(lp);
    }
}
